<?php

class ome_mdl_branch_product extends dbeav_model{
    /*
     * 更新仓库库存
     */
    function change_store($branch_id, $product_id, $num, $operator='='){
        $now = time();
        $store = "";
        switch($operator){
            case "+":
                $store = "store=IFNULL(store,0)+".$num;
                break;
            case "-":
                $store = " store=IF((CAST(store AS SIGNED)-$num)>0,store-$num,0) ";
                break;
            case "=":
            default:
                $store = "store=".$num;
                break;
        }

        $sql = 'UPDATE sdb_ome_branch_product SET '.$store.',last_modified='.$now.' WHERE product_id='.$product_id.' AND branch_id='.$branch_id;
        if($this->db->exec($sql)){
            return $this->count_store($product_id);
        }else{
            return false;
        }
    }

    /*
     * 统计所有此商品库存
     */
    function count_store($product_id, $branch_id=0){
        $this->app->model('products')->count_store($product_id);
        return true;
    }

    /* 减仓库表库存
     * ss备注：货位库存相关方法，可以删除此方法
     */
    function Cut_store($adata){
        $oProducts = &$this->app->model("products");
        $bppObj = &$this->app->model("branch_product_pos");

        foreach($adata['items'] as $k=>$v){
            $bppObj->change_store($adata['branch_id'],$v['product_id'],$v['pos_id'],$v['num'],'-');
       }
    }

    /* 
     * ss备注：货位库存相关方法，可以删除此方法
     */
    function operate_store($adata,$operate){
        $oProducts = &$this->app->model("products");
        $bppObj = &$this->app->model("branch_product_pos");
        
        if($operate=='add')
        {
            $bppObj->change_store($adata['branch_id'],$adata['product_id'],$adata['pos_id'],$adata['num'],'+');
        }else if($operate=='lower'){
            $bppObj->change_store($adata['branch_id'],$adata['product_id'],$adata['pos_id'],$adata['num'],'-');
        }

    }

    /*
     * ss备注：在途库存相关方法，可以删除此方法
     */
    function change_arrive_store($branch_id, $product_id, $num, $type='+'){
        $now = time();
        $store = "";
        switch($type){
            case "+":
                $store = "arrive_store=IFNULL(arrive_store,0)+".$num;
                break;
            case "-":
                $store = " arrive_store=IF((CAST(arrive_store AS SIGNED)-$num)>0,arrive_store-$num,0) ";
                break;
            case "=":
            default:
                $store = "arrive_store=".$num;
                break;
        }
        $sql = 'UPDATE sdb_ome_branch_product SET '.$store.' WHERE product_id='.$product_id.' AND branch_id='.$branch_id;
        $rs = $this->db->exec($sql);
        return $rs;
    }

    /*
     * ss备注：货位相关方法，可以删除此方法
     */
    function Get_pos_id($branch_id,$store_position){
          $obranch_pos = &$this->app->model('branch_pos');
          $pos = $obranch_pos->dump(array('branch_id'=>$branch_id,'store_position'=>$store_position),'pos_id');
          return $pos['pos_id'];
    }

    /*
     * 修改冻结库存
     */
    function chg_product_store_freeze($branch_id,$product_id,$num,$operator='='){
        $now = time();
        $store_freeze = "";
        switch($operator){
            case "+":
                $store_freeze = "store_freeze=IFNULL(store_freeze,0)+".$num.",";
                break;
            case "-":
                $store_freeze = " store_freeze=IF((CAST(store_freeze AS SIGNED)-$num)>0,store_freeze-$num,0),";
                break;
            case "=":
            default:
                $store_freeze = "store_freeze=".$num.",";
                break;
        }
        $sql = 'UPDATE sdb_ome_branch_product SET '.$store_freeze.'last_modified='.$now.' WHERE product_id='.$product_id.' AND branch_id = '.$branch_id;
        $this->db->exec($sql);
    }
    
    /*
     * 增加冻结库存
     */
    function freez($branch_id,$product_id,$nums){
        //暂时没有在branch_product上使用冻结库存
        $this->chg_product_store_freeze($branch_id,$product_id,$nums,"+");       
        return true;
    }
    
    /*
     * 释放冻结库存
     */
    function unfreez($branch_id,$product_id,$nums){
        //暂时没有在branch_product上使用冻结库存
        $this->chg_product_store_freeze($branch_id,$product_id,$nums,"-");       
        return true;
    }
    
	function getStoreByBranch($product_id,$branch_id){
    	$row = $this->db->selectRow('select store from sdb_ome_branch_product where product_id='.$product_id.' and branch_id='.$branch_id);
    	if($row)
    		return $row['store'];
    	else
    		return false;
    }
    
	function getStoreListByBranch($branch_id,$pids){
    	$rows = $this->db->select('select product_id,store from sdb_ome_branch_product where product_id in('.implode(',', $pids).') and branch_id='.$branch_id);
    	if($rows){
    		$products = array();
    		foreach($rows as $row){
    			$products[$row['product_id']] = $row['store'];
    		}
    		
    		return $products;
    	}else{
    		return false;
    	}
    }
}
?>
